package com.power.services.member;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.power.dao.member.TmFamilyinfoDAO;
import com.power.model.member.TmFamilyinfo;

@Service("tmFamilyinfoService")
public class TmFamilyinfoServiceImpl implements TmFamilyinfoService {

	@Autowired
	public TmFamilyinfoDAO dao;

	public void create(TmFamilyinfo arg0) {
		dao.save(arg0);
	}

	public void delete(TmFamilyinfo arg0) {
		dao.delete(arg0);
	}

	public TmFamilyinfo read(Integer arg0) {
		return dao.get(arg0);
	}

	public void update(TmFamilyinfo arg0) {
		dao.update(arg0);
	}

	@SuppressWarnings("unchecked")
	public Map<String, Object> findAll(String type, int start, int limit) {
		List<TmFamilyinfo> disList = null;
		Map<String, Object> map = new HashMap<String, Object>();
		DetachedCriteria criteria = DetachedCriteria
				.forClass(TmFamilyinfo.class);
		if (!"".equals(type))
			criteria.add(Restrictions.eq("isDual", type));
		criteria.addOrder(Order.desc("createTime"));
		disList = dao.findByCriteria(criteria, start, limit);
		int count = dao.getRowCount(criteria);
		if (null == disList)
			disList = new ArrayList<TmFamilyinfo>();
		map.put("data", disList);
		map.put("count", count);
		return map;
	}

	@SuppressWarnings("unchecked")
	public TmFamilyinfo findByMemId(Integer authorId) {
		TmFamilyinfo family = null;
		DetachedCriteria criteria = DetachedCriteria
				.forClass(TmFamilyinfo.class);
		criteria.add(Restrictions.eq("userId", authorId));
		List<TmFamilyinfo> familyList = dao.findByCriteria(criteria);
		if (null != familyList && familyList.size() > 0) {
			family = familyList.get(0);
		}
		return family;
	}

	public void saveOrUpdate(TmFamilyinfo family) {
		if (null == family.getId() || 0 > family.getId())
			dao.save(family);
		else
			dao.update(family);
	}

	public void deleteInfo(String ids) {
		String[] idAry = ids.split("_");
		for (int i = 0; i < idAry.length; i++) {
			Integer id = new Integer(idAry[i]);
			dao.deleteByKey(id);
		}
	}

}
